package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseTeacherDTO;
import com.xuecheng.common.exception.ExceptionCast;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.common.constant.ContentErrorCode;
import com.xuecheng.content.convert.CourseTeacherConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.entity.CourseTeacher;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.CourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程-教师关系表 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseTeacherServiceImpl extends ServiceImpl<CourseTeacherMapper, CourseTeacher> implements CourseTeacherService {


    @Autowired
    private CourseBaseService courseBaseService;

    /*根据课程ID查询教师信
    根据courseBaseId查询courseTeacher表中的教师信息
    判断是否查询成功
    并转DTO返回
     */
    public List<CourseTeacherDTO> queryCourseTeacher(Long courseBaseId) {

        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId, courseBaseId);

        List<CourseTeacher> list = this.list(queryWrapper);
        List<CourseTeacherDTO> courseTeacherDTOS = new ArrayList<>();
        ////存在返回数据，不存在返回null
        if (ObjectUtils.isEmpty(list)) {

            return courseTeacherDTOS;

        } else {

            courseTeacherDTOS = CourseTeacherConvert.INSTANCE.pos2dtos(list);
            return courseTeacherDTOS;

        }


    }

    /*
       新增或修改教师信息
       关键数据 courseId，photograph,position,teacherName，companyId 不为空
       业务数据 查询课程信息 判断是否存在 是否删除 审核状态是否为未提交或审核未通过
       在判断 courseTeacherId 是否存在 存在修改，不存在新建
       新增创建一个courseteacher对象，并保存，并判断是否保存成功
       如果为修改的话，修改关键数据，判修改是否成功
       返回数据
       最后将新建的数据 或 修改的数据 返回给前端
     */
    public CourseTeacherDTO createOrModifyTeacher(CourseTeacherDTO courseTeacherDTO, Long companyId) {
        //courseId，photograph,position,teacherName，companyId
        if (ObjectUtils.isEmpty(companyId) ||
                ObjectUtils.isEmpty(courseTeacherDTO.getCourseId()) ||
                StringUtil.isBlank(courseTeacherDTO.getPhotograph()) ||
                StringUtil.isBlank(courseTeacherDTO.getPosition()) ||
                StringUtil.isBlank(courseTeacherDTO.getTeacherName())) {
            ExceptionCast.cast(ContentErrorCode.E_120023);
        }

        //查询课程信息 判断是否存在 是否删除 审核状态是否为未提交或审核未通过或未审核(另一层已做判断)
        courseBaseService.getCourseBase(courseTeacherDTO.getCourseId(), companyId);

        CourseTeacher teacher = new CourseTeacher();
        //判断 courseTeacherId 是否存在 存在修改，不存在新建
        if (ObjectUtils.isEmpty(courseTeacherDTO.getCourseTeacherId())) {
            //不存在创建
            teacher = createTeacher(courseTeacherDTO);

        } else {
            //存在 修改
            teacher = modifyTeacher(courseTeacherDTO);
        }

        CourseTeacherDTO dto = CourseTeacherConvert.INSTANCE.po2dto(teacher);
        return dto;
    }


    //修改CourseTeacher信息
    private CourseTeacher modifyTeacher(CourseTeacherDTO courseTeacherDTO) {
        CourseTeacher courseTeacher = CourseTeacherConvert.INSTANCE.dto2po(courseTeacherDTO);
        //创建时间不修改
        courseTeacher.setCreateDate(null);
        boolean updateById = this.updateById(courseTeacher);
        if (!updateById) {
            ExceptionCast.cast(ContentErrorCode.E_1200505);
        }
        return courseTeacher;
    }

    //创建CourseTeacher信息
    private CourseTeacher createTeacher(CourseTeacherDTO courseTeacherDTO) {
        CourseTeacher courseTeacher = CourseTeacherConvert.INSTANCE.dto2po(courseTeacherDTO);
        courseTeacher.setCreateDate(LocalDateTime.now());
        boolean save = this.save(courseTeacher);
        if (!save) {
            ExceptionCast.cast(ContentErrorCode.E_1200504);
        }
        return courseTeacher;
    }

    /*
    关键数据 courseBaseId  courseTeacherId 不为空
       业务数据
        课程基本信息 是否存在 是否删除 审核状态是否为未提交或审核未通过或未审核
        根据课程id教师id查询记录，存在删除，不存在抛出异常
        直接删除 绑定信息
        判断删除是否成功
    */
    public void removeTeacher(Long courseBaseId, Long courseTeacherId, Long companyId) {
        //courseBaseId  courseTeacherId 不为空
        if (ObjectUtils.isEmpty(courseBaseId) ||
                ObjectUtils.isEmpty(courseTeacherId) ||
                ObjectUtils.isEmpty(companyId)) {
            ExceptionCast.cast(ContentErrorCode.E_120023);
        }
        //课程基本信息 是否存在 是否删除 审核状态是否为未提交或审核未通过或未审核
        CourseBase courseBase = courseBaseService.getCourseBase(courseBaseId, companyId);

        if (ObjectUtils.isEmpty(courseBase)){
            ExceptionCast.cast(ContentErrorCode.E_120013);
        }

        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseBaseId);
        queryWrapper.eq(CourseTeacher::getId,courseTeacherId);

        //根据课程id教师id查询记录，存在删除，不存在抛出异常
        CourseTeacher teacher = this.getOne(queryWrapper);
        if (ObjectUtils.isEmpty(teacher)){
            //不存在抛出异常
            ExceptionCast.cast(ContentErrorCode.E_1200503);
        }else {
            //存在删除
            //直接删除 绑定信息
            boolean removeTeacher = this.remove(queryWrapper);
            //判断删除是否成功
            if (!removeTeacher){
                ExceptionCast.cast(ContentErrorCode.E_1200506);
            }
        }



    }
}
